/** 
  * @file		mqtt.c
  * @author		周晨
  * @version	V1.0.0
  * @date		26/1/2024
  * @brief		远程报警实验-MQTT
  * @par 描述:
  *         连接mqtt，接收发送消息
 */

/**
  * @brief 头文件 
 */
#include "MQTTClient.h"
#include "mqtt.h"

#include "cmsis_os2.h"
#include "ohos_init.h"
#include <stdio.h>
#include <stdlib.h>
#include "globalvars.h"


MQTTClient client;

char mq_buf[100] = "000000000";
// int g_recdata = 0;

char str[16]; // 确保字符串数组足够大以容纳数字及其可能的负号  
  

/**
 * @brief mqtt消息获取函数
 * 
 * @par 描述:
 *          获取mqtt下发的数据
 * 
 * @param   buf  接收的数据
 */
void Network_mqtt_readdata(char* buf)
{
	memcpy(buf , mq_buf , 100);  //复制MQTT的数据
	// strcpy(buf, (const char*)mq_buf);	//复制MQTT的数据
	memset(mq_buf, 0, 100);		//数组清零
}

/**
  * @brief MQTT发送数据函数
  * @param msg 要发送的消息
  * @retval void
 */
void Network_mqtt_senddata(mqtt_msg msg)
{
	static MQTTMessage ackmsg;
	/* 设置发送参数 */
  	ackmsg.payload = msg.send_data;
  	ackmsg.payloadlen = strlen((char*)ackmsg.payload);
  	ackmsg.qos = QOS0;
  	ackmsg.retained = 0;
  	ackmsg.dup = 0;	
	/* 打印发布的主题及数据 */
	printf("publish topic : %s \r\n",msg.publish);
	printf("publish data : %s \r\n",msg.send_data);
	//往mqtt发送数据
	MQTTPublish(&client, msg.publish, &ackmsg);
}

/**
 * @brief mqtt消息接收函数
 * 
 * @par 描述:
 *          解析mqtt下发的消息
 * 
 * @param   md  mqtt下发的消息
 */
void Network_mqtt_message(MessageData* md)
{
 	MQTTMessage* message = md->message;
 	/** 打印接收到的信息 **/
	printf("%.*s\n", (int)message->payloadlen, (char*)message->payload);

	memcpy(mq_buf , message->payload , message->payloadlen); 
	// g_recdata = atoi(mq_buf);
	// printf("data_all:%d\n", data_all);
	// printf("")
	// strcpy(mq_buf, (const char*)message->payload);	//复制接收到的数据
}

/**
 * @brief mqtt主题订阅函数
 * 
 * @par 描述:
 *          订阅mqtt主题
 * 
 * @param   mqtt_opts  需要订阅的主题
 */
void Network_mqtt_subscriber(mqtt_msg mqtt_opts)
{
	int ret;
	// abc;
	/* 订阅主题及接收数据 */
	ret = MQTTSubscribe(&client, mqtt_opts.subscriber, QOS0, Network_mqtt_message);
	if (ret != 0)
	{
		//mqtt主题订阅失败
		printf("MQTT subscribed failed!!!\r\n");
	}
	//mqtt主题订阅成功
	printf("MQTT subscribed successful!!!\r\n");
}
// data10 = 10;
/**
 * @brief mqtt连接函数
 * 
 * @par 描述:
 *          连接mqtt
 */
void Network_mqtt_connect(mqtt_msg mqtt_opts)
{
	int ret = 0;
	uint8_t sendbuf[100];  //数据发送内存
	uint8_t readbuf[100];  //数据接收内存

	/* MQTT初始化 */
	Network mqtt_net;  //定义mqtt服务器相关结构体
	NetworkInit(&mqtt_net);  //mqtt初始化
	//连接MQTT服务器
	NetworkConnect(&mqtt_net, mqtt_opts.host, mqtt_opts.port); 
	printf("Connecting to %s %d\n", mqtt_opts.host, mqtt_opts.port); 
	//创建MQTT客户端
	MQTTClientInit(&client, &mqtt_net, 1000, sendbuf, 100, readbuf, 100);
	MQTTStartTask(&client);//启动MQTT任务

	/* 设置MQTT客户端相关参数 */
	MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
	data.willFlag = 0;
	data.MQTTVersion = 3;
	data.clientID.cstring = mqtt_opts.clientid;
	data.username.cstring = mqtt_opts.username;
	data.password.cstring = mqtt_opts.password;
	data.keepAliveInterval = 10;
	data.cleansession = 1;
	/* 连接到MQTT服务器 */
	ret = MQTTConnect(&client, &data);
	if (ret != 0){
		//mqtt连接失败
		printf("MQTT connected failed!!!\r\n");
	}
	//mqtt连接成功
	printf("MQTT connected successful!!!\r\n");

	//订阅主题和接收消息
	Network_mqtt_subscriber(mqtt_opts);
	
	/* 发送测速数据 */
	mqtt_opts.publish = "S_send";  //发布消息主题

	  //发布测速数据
	// mqtt_opts.send_data = "this is mqtt test";
	// int num = 0;
	
	// sprintf(str, "%f", g_temp);
	// mqtt_opts.send_data = g_senddata;
	// Network_mqtt_senddata(mqtt_opts);
	/* 程序处于接收消息状态 */
	char temp_str[5];
	char humi_str[5];
	char gas_str[5];
	char door_alarm_flag[1];
	while (1)
	{
		// mqtt_opts.send_data = "this is mqtt test";
		// int nums = num;
		// if (num != nums)
		// {
		// mqtt_opts.send_data = "222";
		// Network_mqtt_senddata(mqtt_opts);
		// mqtt_opts.send_data = "333";
		// Network_mqtt_senddata(mqtt_opts);
		// mqtt_opts.send_data = "444";
		sprintf(temp_str, "%.2f", g_temp);
		sprintf(humi_str, "%.2f", g_humi);
		sprintf(gas_str, "%.2f", g_gas);
		sprintf(door_alarm_flag, "%d", g_door_alarm_status);
		sprintf(str, "%s%s%s%s", temp_str, humi_str, gas_str, door_alarm_flag);
		// // // printf("wen::::::::::::::::::::::::%f", g_temp);
		mqtt_opts.send_data = str;
		Network_mqtt_senddata(mqtt_opts);
		// }
		// printf("11111111111111111");
		sleep(2);  
	}
}

